<template>
  <div>
    <NavBar :title="title" />
    <div class="content-top">

      <div class="resume-edit-basic">
        <!-- 基本信息 -->
        <van-field v-model="basicInfoPostData.PersonName" size="large" label="姓名" placeholder="请填写" style="font-size: 16px" />
        <!-- 性别 -->
        <van-field size="large" label="性别" style="font-size: 16px">
          <template #input>
            <van-radio-group v-model="basicInfoPostData.Sex" direction="horizontal">
              <van-radio name="男" checked-color="#05A6F0" icon-size="16px">先生</van-radio>
              <van-radio name="女" checked-color="#05A6F0" icon-size="16px">女士</van-radio>
            </van-radio-group>
          </template>
        </van-field>
        <!-- 出生日期 -->
        <van-field readonly clickable size="large" :value="basicInfoPostData.BirthDate" label="出生日期" placeholder="请选择" style="font-size: 16px" @click="showPicker_birth = true" />
        <van-popup v-model="showPicker_birth" round position="bottom">
          <van-datetime-picker v-model="picker_birth_current" :min-date="picker_birth_minDate" :max-date="picker_birth_maxDate" type="date" title="出生日期" @confirm="onPickerBirthSubmit" @cancel="showPicker_birth = false" />
        </van-popup>
        <!-- 学历 -->
        <van-field readonly clickable size="large" :value="basicInfoPostData.Level_EduValue" label="最高学历" placeholder="请选择" style="font-size: 16px" @click="showPicker_edu = true" />
        <van-popup v-model="showPicker_edu" round position="bottom">
          <van-picker title="最高学历" show-toolbar :columns="picker_edu_data" :default-index="picker_edu_defaultIndex" value-key="codeValue" @confirm="onPickerEduSubmit" @cancel="showPicker_edu = false" />
        </van-popup>

        <van-field readonly clickable size="large" :value="basicInfoPostData.Work_yearsValue != '' ? basicInfoPostData.Work_yearsValue : ''" label="工作年限" placeholder="请选择" style="font-size: 16px" @click="showPicker_workyears = true" />
        <van-popup v-model="showPicker_workyears" round position="bottom">
          <van-picker title="工作年限" show-toolbar :columns="picker_workyears_data" :default-index="picker_joinWorkYear_defaultIndex" value-key="codeValue" @confirm="onPickerWorkyearsSubmit" @cancel="showPicker_joinWorkYear = false" />
        </van-popup>

        <!-- 意向 -->
        <van-cell-group>
          <!-- 职位 -->
          <div class="subscribe-job">
            <p>
              <label>意向岗位</label>
              <span @click="showPicker_zn = true">{{
                selected_zn.length > 0 ? "修改" : "添加"
              }}</span>
            </p>
            <ul>
              <li v-for="(item, index) in selected_znName" :key="index">
                {{ item }}
              </li>
            </ul>
          </div>
          <van-popup v-model="showPicker_zn" position="bottom" style="height: 100%">
            <div class="jobs-picker">
              <!-- 标题头 -->
              <div class="top">
                <p>
                  <button type="button" @click="showPicker_zn = false">
                    取消
                  </button>
                  <span>选择期望职位（最多3个）</span>
                  <button type="button" @click="onZNSubmit">确认</button>
                </p>
                <!-- 搜索框 -->
                <van-search v-model="kw_zn" clear-trigger="always" @search="onZNSearch" @cancel="onZNSearchClear" shape="round" placeholder="请输入搜索关键词" style="border-bottom: 0.5px solid #e8e8e8" />
              </div>
              <!-- 搜索结果 -->
              <ul class="result" v-if="selected_zn_search.length > 0">
                <li v-for="(item, index) in selected_zn_search" :key="index" @click="onZNTwoClick(item)">
                  <p v-html="item.codeValue.replace(kw_zn, '<span style=color:red>' + kw_zn + '</span>')"></p>
                  <p>{{ item.title }}</p>
                </li>
              </ul>
              <!-- 选项 -->
              <div class="branch" v-if="selected_zn_search.length === 0">
                <div class="popu">
                  <!-- 一级 -->
                  <ul class="second">
                    <li v-for="(item, index) in select_zn_one" :key="index" @click="onZNOneClick(item)">
                      {{ item.codeValue }}
                    </li>
                  </ul>

                  <!-- 二级 -->
                  <ul class="three">
                    <li :class="{ select: selected_zn.indexOf(item.code) != -1 }" v-for="(item, index) in select_zn_two" :key="index" @click="onZNTwoClick(item)">
                      {{ item.codeValue }}
                    </li>
                  </ul>
                </div>
                <!-- <div class="shade" v-if="select_zn_one_id != ''" @click="select_zn_one_id = ''"></div> -->
                <!-- <div class="popu" v-if="select_zn_one_id != ''">
                 
                </div> -->
              </div>
              <!-- 已选 -->
              <div class="selected" v-if="selected_znName_sel.length > 0">
                <label>已选：</label>
                <ul>
                  <li v-for="(item, index) in selected_znName_sel" :key="index" @click="onZNSelRemove(index)">
                    {{ item }}
                  </li>
                </ul>
              </div>
            </div>
          </van-popup>
          <!-- 地区 -->
          <van-cell title="期望地区" :value="select_pick_area" is-link size="large" @click="showPicker_area = true" />
          <van-popup v-model="showPicker_area" round position="bottom">
            <div class="subscribe-area-picker">
              <p>
                <button type="button" @click="showPicker_area = false">
                  取消
                </button>
                <span>选择期望地区（多选）</span>
                <button type="button" @click="onAreaSubmit">确认</button>
              </p>
              <ul>
                <li :class="{ selected: selected_area.indexOf(item.code) >= 0 }" v-for="(item, index) in select_area" :key="index" @click="onAreaSelect(item)">
                  {{ item.codeValue }}
                </li>
              </ul>
            </div>
          </van-popup>
          <!-- 薪资 -->
          <van-cell title="期望薪资" :value="picker_monthpay_val" @click="showPicker_monthpay = true" is-link size="large" />
          <van-popup v-model="showPicker_monthpay" :lazy-render="false" round position="bottom">
            <van-picker title="期望薪资" show-toolbar :columns="picker_monthpay_data" :default-index="picker_monthpay_defaultIndex" @confirm="onPickMonthPayConfirm" @cancel="showPicker_monthpay = false" value-key="codeValue" />
          </van-popup>
        </van-cell-group>

        <!-- <van-field type="password" v-model="postData.password" size="large" maxlength="10" label="设置密码" placeholder="请填写" style="font-size: 16px" /> -->

        <div style="margin: 16px">
          <van-button type="primary" block color="#05A6F0" round style="font-size: 18px" text="提交" :disabled="disabled_submit" :loading="loading_submit" @click="onSubmit">
          </van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import NavBar from "@/components/NavBar.vue";
import { Field, RadioGroup, Radio, Picker, Popup, Button, DatetimePicker, Toast, Cascader, CellGroup, Search, Cell, } from "vant";
import { getMobileNum, setResume } from '@/util/session'

export default {
  name: "ResumeQuick1",
  components: {
    NavBar,
    [Field.name]: Field,
    [Radio.name]: Radio,
    [RadioGroup.name]: RadioGroup,
    [Picker.name]: Picker,
    [Popup.name]: Popup,
    [Button.name]: Button,
    [DatetimePicker.name]: DatetimePicker,
    [Cascader.name]: Cascader,
    [CellGroup.name]: CellGroup,
    [Search.name]: Search,
    [Cell.name]: Cell,
  },
  props: ["title"],
  // 定义属性
  data() {
    return {
      basicInfoPostData: {
        PersonName: "",
        Sex: "",
        BirthDate: "",
        Level_EduValue: "",
        Level_Edu: "",
        Work_years: '',
        Work_yearsValue: '',
        Tel_Home: getMobileNum(),
      },
      jobWantedPostData: {
        Position_ID: "",//Position_ID
        Work_Addr: "",//Work_Addr
        Salary_Wanted: "", //Salary_Wanted
      },
      showPicker_birth: false,
      picker_birth_current: new Date(),
      picker_birth_minDate: new Date(new Date().getFullYear() - 70, 0, 1),
      picker_birth_maxDate: new Date(new Date().getFullYear() - 16, 0, 1),

      showPicker_edu: false,
      picker_edu_data: [],
      picker_edu_defaultIndex: 0,

      showPicker_joinWorkYear: false,
      picker_joinWorkYear_defaultIndex: 0,

      picker_workyears_data: [],
      pick_workyears_defaultIndex: 0,
      showPicker_workyears: false,

      showPicker_zn: false,
      kw_zn: "",
      bd_zn_one: [],//所有一级分类只能数据
      bd_zn_two: [],//所有二级分类只能数据
      select_zn_one: [],
      select_zn_one_id: "",
      select_zn_two: [],
      select_zn_tow_id: '',
      selected_zn_search: [],
      selected_zn: [],
      selected_znName: [], //选择的 点确定后 显示的
      selected_znName_sel: [], //选择的

      showPicker_area: false,
      select_area: [],
      selected_area: [],
      selected_areaName: [],
      select_pick_area: "请选择",

      showPicker_monthpay: false,
      picker_monthpay_data: [],
      picker_monthpay_defaultIndex: 0,
      picker_monthpay_val: '请选择',

      loading_submit: false,
    };
  },
  created() {
    //init base data
    this.$api.get('/BaseData/GetBaseDataForResumeQuick1UI').then(res => {
      this.picker_edu_data = res.edu;
      this.picker_workyears_data = res.workYears;
      this.bd_zn_one = this.select_zn_one = res.positionGrade1;
      this.bd_zn_two = this.select_zn_two = res.positionGrade2;
      this.select_area = res.area;
      this.picker_monthpay_data = res.monthpay;
    })
  },
  mounted() {
    this.getInfo();
  },
  computed: {
    disabled_submit() {
      if (
        this.basicInfoPostData.PersonName != "" &&
        this.basicInfoPostData.Sex != "" &&
        this.basicInfoPostData.BirthDate != "" &&
        this.basicInfoPostData.Level_Edu != "" &&
        this.jobWantedPostData.Position_ID != "" &&
        this.jobWantedPostData.Work_Addr != "" &&
        this.jobWantedPostData.Salary_Wanted != ""
        // this.postData.password != ""
      )
        return false;
      return true;
    },
  },
  // 方法集合
  methods: {
    getInfo() {
      this.$api.get('/Resume/GetInfoAtResumeQuick1UI').then(res => {
        if (res.success && res.response != null) {
          var data = res.response;
          //提交数据赋值
          this.basicInfoPostData.PersonName = data.personName;
          this.basicInfoPostData.Sex = data.sex;
          this.basicInfoPostData.BirthDate = data.birthDate;
          this.basicInfoPostData.Level_EduValue = data.level_EduValue;
          this.basicInfoPostData.Level_Edu = data.level_Edu;
          this.basicInfoPostData.Work_years = data.work_years;
          this.basicInfoPostData.Work_yearsValue = data.work_yearsValue;
          this.jobWantedPostData.Position_ID = data.jW_PositionCodeArr.join();
          this.jobWantedPostData.Work_Addr = data.jW_AreaCodeArr.join();
          this.jobWantedPostData.Salary_Wanted = data.jW_SalaryCode;
          //显示数据赋值
          this.selected_zn = data.jW_PositionCodeArr;
          this.selected_znName = data.jW_PositionValueArr;
          this.selected_znName_sel = data.jW_PositionValueArr;
          this.select_pick_area = data.jW_AreaValueArr.join();
          this.selected_area = data.jW_AreaCodeArr;
          this.selected_areaName = data.jW_AreaValueArr;
          this.picker_monthpay_val = data.jW_SalaryValue;

        }
      })
    },
    //生日
    onPickerBirthSubmit(value) {
      this.showPicker_birth = false;
      this.basicInfoPostData.BirthDate = this.$dayjs(value).format("YYYY-MM-DD");
    },
    //学历
    onPickerEduSubmit(item) {
      this.showPicker_edu = false;
      this.basicInfoPostData.Level_EduValue = item.codeValue;
      this.basicInfoPostData.Level_Edu = item.code;
    },
    //工作时间
    onPickerWorkyearsSubmit(item) {
      this.showPicker_workyears = false; this.basicInfoPostData.Work_years = item.code; this.basicInfoPostData.Work_yearsValue = item.codeValue;
    },
    //职能大类选择
    onZNOneClick(item) {
      this.select_zn_one_id = item.code;
      this.select_zn_two = this.bd_zn_two.filter((item_two) => {
        return item_two.code.substring(0, 2) === item.code.substring(0, 2);
      });
    },
    //职能中类选择
    onZNTwoClick(item) {
      this.select_zn_tow_id = item.code;
      if (this.selected_zn.length === 3) {
        Toast("最多选择3个意向岗位");
        return;
      }
      if (this.selected_zn.indexOf(item.code) >= 0) {
        Toast("该意向岗位已经选择");
        return;
      }
      this.selected_zn.push(item.code);
      this.selected_znName_sel.push(item.codeValue);
    },
    //删除已选职能
    onZNSelRemove(index) {
      this.selected_znName_sel.splice(index, 1);
      this.selected_zn.splice(index, 1);
    },
    //搜索职能
    onZNSearch(val) {
      this.selected_zn_search = [];
      if (val === "") {
        Toast("请输入关键字");
        return;
      }
      if (val === "所有" || val === "其他") {
        Toast("搜索不到该岗位");
        return;
      }
      //从二级类别中搜索
      let array_result = this.bd_zn_two.filter((item) => {
        return item.codeValue.indexOf(val) >= 0;
      });
      //遍历搜索结果找出该类分类
      if (array_result.length > 0) {
        array_result.forEach((item) => {
          let item_tow = this.bd_zn_two.filter((i) => {
            return i.code === item.code.substr(0, 4);
          })[0];
          let item_one = this.bd_zn_one.filter((i) => {
            return i.code.substr(0, 2) === item.code.substr(0, 2);
          })[0];
          item.title = `${item_one.codeValue}-${item_tow.codeValue}`;
        });
        this.selected_zn_search = array_result;
      } else {
        Toast("搜索不到该岗位");
        return;
      }
    },
    //删除搜索关键字
    onZNSearchClear() {
      Toast('1')
      this.selected_zn_search = [];
    },
    //职能选择确认
    onZNSubmit() {
      if (this.selected_zn.length === 0) {
        Toast("请选择意向岗位");
        return;
      }
      this.showPicker_zn = false;
      let nameStr = JSON.stringify(this.selected_znName_sel);
      this.selected_znName = JSON.parse(nameStr);
      this.jobWantedPostData.Position_ID = this.selected_zn.join();
    },
    //选择地区
    onAreaSelect(item) {
      let index = this.selected_area.indexOf(item.code);
      if (index >= 0) {
        this.selected_area.splice(index, 1);
        this.selected_areaName.splice(index, 1);
      } else {
        if (this.selected_area.length === 3) {
          Toast("最多选3个");
          return;
        }
        this.selected_area.push(item.code);
        this.selected_areaName.push(item.codeValue);
      }
    },
    //选择地区-确定
    onAreaSubmit() {
      if (this.selected_area.length === 0) {
        Toast("请选择");
        return;
      }
      this.select_pick_area = this.selected_areaName.join();
      this.jobWantedPostData.Work_Addr = this.selected_area.join();
      this.showPicker_area = false;
    },
    //选择薪资
    onPickMonthPayConfirm(item) {
      this.showPicker_monthpay = false;
      this.jobWantedPostData.Salary_Wanted = item.code;
      this.picker_monthpay_val = item.codeValue;
    },
    //保存
    onSubmit() {
      let _this = this;
      this.loading_submit = true;
      // console.log(this.postData);
      this.$api.axios.all([this.saveBasicInfo(), this.saveJobWantedInfo()]).then(_this.$api.axios.spread((res1, res2) => {
        this.loading_submit = false;
        if (res1.success && res2.success) {
          Toast.success('保存成功');
          setResume();
          this.$router.push({ name: "person-resume-resumequick2" });
        }
        else {
          Toast.fail(res1.msg + res2.msg);
        }
      }))
    },


    saveBasicInfo() {
      return this.$api.post('/Resume/SaveBasicInfo', this.basicInfoPostData);
    },
    saveJobWantedInfo() {
      return this.$api.post('/Resume/SaveJobWanted', this.jobWantedPostData);
    }
  },
};
</script>

<style scoped>
.resume-edit-basic .more-btn {
  display: flex;
  justify-content: center;
  margin-top: 14px;
  font-size: 15px;
  color: #05a6f0;
}

.resume-edit-basic .more-select {
  margin-top: 10px;
}

.subscribe-job {
  position: relative;
  display: flex;
  flex-direction: column;
  padding: 12px 15px;
  background-color: #fff;
}

.subscribe-job::after {
  content: "";
  position: absolute;
  left: 15px;
  right: 15px;
  bottom: 0;
  height: 0.5px;
  background-color: #ebedf0;
}

.subscribe-job p {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 22px;
  line-height: 22px;
}

.subscribe-job p label {
  font-size: 16px;
  color: #323233;
}

.subscribe-job p label::after {
  content: "（最多选3个）";
  font-size: 13px;
  color: #888;
}

.subscribe-job p span {
  padding: 0 10px;
  border-radius: 12px;
  background-color: #ff7700;
  font-size: 13px;
  color: #fff;
}

.subscribe-job ul {
  display: flex;
  flex-wrap: wrap;
  margin-top: 12px;
}

.subscribe-job ul li {
  display: flex;
  align-items: center;
  height: 26px;
  padding: 0 8px;
  border: 1px solid #ff7700;
  border-radius: 30px;
  margin: 4px 6px 4px 0;
  font-size: 13px;
  color: #05a6f0;
}

.jobs-picker {
  position: relative;
  background-color: #fff;
  -webkit-user-select: none;
  user-select: none;
}

.jobs-picker .top {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
  display: flex;
  flex-direction: column;
  background-color: #fff;
}

.jobs-picker .top p {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 44px;
  border-bottom: 0.5px solid #e8e8e8;
}

.jobs-picker .top p span {
  max-width: 50%;
  font-weight: 500;
  font-size: 16px;
  line-height: 20px;
  text-align: center;
}

.jobs-picker .top p button {
  height: 100%;
  padding: 0 16px;
  font-size: 14px;
  background-color: transparent;
  border: none;
  cursor: pointer;
}

.jobs-picker .top p button:nth-child(1) {
  color: #969799;
}

.jobs-picker .top p button:nth-child(3) {
  color: #576b95;
}

.jobs-picker .selected {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 888;
  display: flex;
  height: 32px;
  padding: 10px 15px;
  border-top: 0.5px solid #e8e8e8;
  background-color: #fff;
}

.jobs-picker .selected label {
  display: flex;
  align-items: center;
  flex: 0 0 42px;
  height: 32px;
  font-size: 14px;
  color: #333;
}

.jobs-picker .selected ul {
  display: flex;
  align-items: center;
  flex: 1;
  height: 32px;
  margin: 0;
  overflow: hidden;
}

.jobs-picker .selected ul li {
  position: relative;
  max-width: 32%;
  height: 30px;
  padding: 0 15px 0 8px;
  border: 1px solid rgba(255, 119, 0, 0.25);
  border-radius: 30px;
  background-color: rgba(255, 119, 0, 0.05);
  font-size: 13px;
  color: #05a6f0;
  line-height: 30px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  box-sizing: border-box;
}

.jobs-picker .selected ul li + li {
  margin-left: 2%;
}

.jobs-picker .selected ul li::before,
.jobs-picker .selected ul li::after {
  content: "";
  position: absolute;
  top: 50%;
  right: 11px;
  width: 1px;
  height: 10px;
  background-color: #05a6f0;
}

.jobs-picker .selected ul li::before {
  transform: translateY(-50%) rotate(-45deg);
}

.jobs-picker .selected ul li::after {
  transform: translateY(-50%) rotate(45deg);
}

.jobs-picker .result {
  padding-left: 15px;
  margin: 99px 0 52.5px;
}

.jobs-picker .result li {
  display: flex;
  flex-direction: column;
  padding: 10px 16px 10px 0;
  border-bottom: 0.5px solid #e8e8e8;
}

.jobs-picker .result li p:nth-child(1) {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.jobs-picker .result li p:nth-child(2) {
  margin-top: 2px;
  font-size: 12px;
  color: #aaa;
}

.jobs-picker .result li p span {
  color: #05a6f0;
}

.jobs-picker .branch {
  margin: 99px 0 52.5px;
}

.jobs-picker .first {
  display: flex;
  flex-direction: column;
  padding-left: 15px;
}

.jobs-picker .first li {
  display: flex;
  align-items: center;
  height: 46px;
  border-bottom: 0.5px solid #e8e8e8;
  font-size: 16px;
  color: #333;
}

.jobs-picker .shade {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  background-color: rgba(0, 0, 0, 0.5);
}

.jobs-picker .popu {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 333;
  display: flex;
  width: 90%;
  padding: 99px 0 52.5px;
  background-color: #fff;
  animation-name: bounceIn;
  animation-fill-mode: both;
  animation-duration: 0.3s;
  overflow-y: auto;
}

@keyframes bounceIn {
  0% {
    opacity: 0;
    transform: translateX(44%);
  }

  100% {
    opacity: 1;
    transform: translateX(0);
  }
}

.jobs-picker .second {
  display: flex;
  flex-direction: column;
  width: 50%;
  overflow-y: scroll;
}

.jobs-picker .second li {
  position: relative;
  display: block;
  padding: 12px 16px;
  font-size: 15px;
  color: #666;
  line-height: 21px;
}

.jobs-picker .second li.select {
  color: #ff7700;
}

.jobs-picker .second li.select::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 0;
  width: 4px;
  height: 20px;
  background-color: #ff7700;
  transform: translateY(-50%);
}

.jobs-picker .three {
  display: flex;
  flex-direction: column;
  width: 50%;
  background-color: #f7f7f7;
  overflow-y: scroll;
}

.jobs-picker .three li {
  position: relative;
  display: block;
  padding: 12px 16px;
  font-size: 15px;
  color: #666;
  line-height: 21px;
}

.jobs-picker .three li.select {
  color: #05a6f0;
}

.subscribe-area-picker {
  position: relative;
  background-color: #fff;
  -webkit-user-select: none;
  user-select: none;
}

.subscribe-area-picker p {
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  align-items: center;
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  justify-content: space-between;
  height: 44px;
  background-color: #fff;
}

.subscribe-area-picker p span {
  max-width: 50%;
  font-weight: 500;
  font-size: 16px;
  line-height: 20px;
  text-align: center;
}

.subscribe-area-picker p button {
  height: 100%;
  padding: 0 16px;
  font-size: 14px;
  background-color: transparent;
  border: none;
  cursor: pointer;
}

.subscribe-area-picker p button:nth-child(1) {
  color: #969799;
}

.subscribe-area-picker p button:nth-child(3) {
  color: #576b95;
}

.subscribe-area-picker ul {
  display: flex;
  flex-wrap: wrap;
  padding: 15px;
}

.subscribe-area-picker ul li {
  display: flex;
  justify-content: center;
  align-items: center;
  width: calc(33.33% - 14px);
  height: 34px;
  border: 1px solid #f9f9f9;
  border-radius: 32px;
  margin: 6px;
  background-color: #f9f9f9;
  font-size: 15px;
  color: #333;
}

.subscribe-area-picker ul li.selected {
  border-color: rgba(255, 119, 0, 0.1);
  background-color: rgba(255, 119, 0, 0.08);
  color: #05a6f0;
  font-weight: 500;
}
</style>
